﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Drawing;
public partial class Bidder_CompnayGoodsAdd : System.Web.UI.Page
{
    Int64 ProductId;
    public string strSql, status;
    Int64 Accountid_QY;
    protected void Page_Load(object sender, EventArgs e)
    {
        int _intProductAddStatus = 0;
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
           
            if (!IsPostBack)
            {
                labProductAddStatus.Text = "0";
                labProductAddStatus.Visible = false;
                if (Request.QueryString["ProductId"] == null)
                {
                    Response.Write("没有要添加的商品编码！！！");
                }
                else
                {
                    //string goodscount;
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    if (_Account != null)
                    {
                        Accountid_QY = SysFun.ToLong(_Account.AccountId);
                        ProductId = Convert.ToInt64(SysFun.IsSQL(Request.QueryString["ProductId"].ToString()));
                        string ComName = "";
                        Db db = new Db();
                        System.Data.IDataReader dr;
                        //取得该企业的名称

                        strSql = "select CompanyName,ProductAddStatus from company where Accountid='" + Accountid_QY.ToString() + "'";
                        dr = db.DataReader(strSql);
                        while (dr.Read())
                        {
                            ComName = dr["CompanyName"].ToString();
                            _intProductAddStatus = SysFun.ToInt(dr["ProductAddStatus"].ToString());
                        }
                        dr.Close();
                        //取得商品信息
                        strSql = "select * from product where Productid='" + ProductId.ToString() + "'";
                        dr = db.DataReader(strSql);
                        while (dr.Read())
                        {
                           
                            labProductCode.Text = dr["productid"].ToString();
                           
                            labProductName.Text = SysFun.ToTrim(dr["productname"].ToString());//商品通用名
                            string _strMedicinemodel = dr["medicinemodel"].ToString();
                            string _strOutlookc = dr["outlookc"].ToString();
                            labMedicinemodel.Text = _strMedicinemodel;
                            labOutlookc.Text = _strOutlookc;
                            if (_strMedicinemodel == "大输液" && _strOutlookc.IndexOf("玻瓶") == -1)
                            {
                                Label2.Text = Label2.Text + "(大容量制剂（治疗用大输液）未标明包材的为玻瓶。)";
                                Label2.BackColor = Color.Yellow; ;
                            }
                            //labOutlookc.Text = SysFun.ToTrim(dr["outlookc"].ToString());//商品规格
                            //labMedicinemodel.Text = dr["medicinemodel"].ToString();//商品剂型
                          
                            txtCompany.Text = ComName;//生产企业名称
                            chkIsImport.Checked = false;//默认非进口药
                            status = "none";//默认不显示
                            chkIsSurveyReport.Checked = false;//默认药品检验报告为无
                            txtPackUnit.Text = SysFun.ToTrim(dr["unit"].ToString());
                        }
                        db.Close();
                        dr.Close();
                        dr.Dispose();
                        txtPackUnit.Visible = false;
                        labProductAddStatus.Text = _intProductAddStatus.ToString();
                        if (_intProductAddStatus != 1)
                        {                           
                            Response.Write("<script language=JavaScript>alert('您暂时没有权限增加品种！如需帮助请于招标中心联系！！！');window.close();</script>");

                        }

                    }
                }
            }
        }
    }


    protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (labProductAddStatus.Text == "1")
        {
            addGoods();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.opener.location.reload();window.close();</script>");
    }
    private void addGoods()
    {
        Int64 IsSalerId_dl = 0;
        DateTime ImportRegistDate = Convert.ToDateTime("1900-1-1");
        HZ.MBSM.Model.Goods goods = new HZ.MBSM.Model.Goods();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {

            ProductId = Convert.ToInt64(SysFun.IsSQL(Request.QueryString["ProductId"].ToString()));//商品号
            Db db = new Db();

            int rowCun = 0;

            System.Data.IDataReader dr;
            strSql = "select count(GOODSID) as cun from dbo.GOODS where PRODUCTID='" + SysFun.ToTrim(ProductId.ToString()) + "'  AND  FACTOR= '" + SysFun.ToTrim(SysFun.ToInt(txtFactor.Text)) + "' AND ACCOUNTID_SC=" + _Account.AccountId.ToString();
            dr = db.DataReader(strSql);
            if (dr.Read())
            {
                rowCun = SysFun.ToInt(dr["cun"]);
            }
            dr.Close();
            dr.Dispose();
            db.Close();
            if (rowCun > 0)
            {
                Response.Write("<script language=JavaScript>alert('该商品在数据库中已经存在！！！');window.close();</script>");
            }
            else
            {
                if (chkIsImport.Checked == true)
                {
                    IsSalerId_dl = SysFun.ToLong(Request.Form["txtIsSalerId_dl"]);
                    goods.ImportCode = txtImportCode.Text.ToString();
                    goods.AccountId_Agent = IsSalerId_dl;// Convert.ToInt64(txtIsSalerId.Text.ToString());
                    if (txtImportRegistDate.Text != "")
                        goods.ImportRegistDate = Convert.ToDateTime(txtImportRegistDate.Text.ToString());
                    else
                        goods.ImportRegistDate = Convert.ToDateTime("1900-1-1");

                }
                
                goods.AccountId_SC = _Account.AccountId;
                goods.AccountId_TJR = _Account.AccountId;//添加人编码
                goods.ProductId = ProductId;
                goods.AccountName_TJR = _Account.AccountName;
                goods.CheckStatus = "0";
                goods.Factor = SysFun.ToInt(txtFactor.Text.ToString());//转换系数
                goods.G_unit = SysFun.ToTrim(txtunit.Text);//制剂单位；
                goods.PackUnit = SysFun.ToTrim(packUnit.Text);//包装单位；
                goods.GoodsInstruction = txtGoodsInstruction.Text.ToString();
                goods.GoodsName = txtGoodsName.Text.ToString();

                goods.IsSurveyReport = chkIsSurveyReport.Checked;
                goods.IsImport = chkIsImport.Checked;
                goods.LicenseNumber = txtLicenseNumber.Text.ToString();
                if (txtLicenseNumberDate.Text != "")
                    goods.LicenseNumberDate = Convert.ToDateTime(txtLicenseNumberDate.Text.ToString());
                else
                    goods.LicenseNumberDate = Convert.ToDateTime("1900-1-1");

                goods.Material = txtMaterial.Text.ToString();
                goods.Pack = "";
                goods.PackUnit = txtPackUnit.Text.ToString();
                
                goods.QualityLevels = "3";
                if (txtRetailPrice.Text != "")
                    goods.RetailPrice = Convert.ToDouble(txtRetailPrice.Text.ToString());
                if (txtSurveyReportDate.Text != "")
                    goods.SurveyReportDate = Convert.ToDateTime(txtSurveyReportDate.Text.ToString());
                else
                    goods.SurveyReportDate = Convert.ToDateTime("1900-1-1");
                goods.SurveyReportId = txtSurveyReportId.Text.ToString();
                goods.memo4 = SysFun.ToTrim(txtMemo4.Text);
                goods.ProductNameSM = txtProductNameSM.Text;
                //add JZdate by ws
                goods.Jzdate = txtjz.Text;
               
                
                //添加GMP信息
                HZ.MBSM.BLL.Goods goodsOperator = new HZ.MBSM.BLL.Goods();

                if (goodsOperator.Add(goods))
                {
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "goods", goods.GoodsId + goods.GoodsName + goods.GroupId + goods.ImportCode + goods.Licence + goods.LicenseNumberDate + goods.Material + goods.NewMedicine + goods.NewMedicineDate + goods.Pack + goods.PackUnit + goods.ProductId + goods.QualityLevels + goods.RetailPrice, "../Magage/goodsadd.aspx");
                    Response.Write("<script>alert('保存成功！！！');window.close();</script>");
                }
                else
                {
                    Response.Write("<script>alert('保存失败！！！');</script>");
                }
            }
        }
        else
        {
            Response.Write("<script language=JavaScript>alert('您无权添加商品！！！');</script>");
        }
    }
}
